<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="box">
    <p class="p">hello world</p>
    <div class="person">
        <span class="name">DOM2JSON</span>
        <span class="age">100</span>
    </div>
</div>
<script>

    const dom = document.querySelector('.box')
    function DOM2JSON(tree) {
        if (!tree) {
            return
        }
        let rootObj = {
            tagName : tree.tagName,
            children: []
        }
        const children = tree.children
        if (children) {
            Array.from(children).forEach((item,index) => {
                rootObj.children[index] = DOM2JSON(item)
            })
        }
        return rootObj
    }
    console.log(DOM2JSON(dom));
</script>
</body>
</html>
